package com.huanong.it.fooder.service.impl;

import com.huanong.it.fooder.dto.UserLoginDto;
import com.huanong.it.fooder.dto.UserRegisterDto;
import com.huanong.it.fooder.dto.UserResponseDto;
import com.huanong.it.fooder.entity.User;
import com.huanong.it.fooder.exception.UserAlreadyExistsException;
import com.huanong.it.fooder.exception.UserNotFoundException;
import com.huanong.it.fooder.repository.UserRepository;
import com.huanong.it.fooder.response.ApiResponse;
import com.huanong.it.fooder.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;

import java.util.Optional;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Service
public class UserServiceImpl implements UserService {

    private final UserRepository userRepository;
    private final PasswordEncoder passwordEncoder;

    @Autowired
    public UserServiceImpl(UserRepository userRepository) {
        this.userRepository = userRepository;
        this.passwordEncoder = new BCryptPasswordEncoder();
    }

    @Override
    public ApiResponse<UserResponseDto> register(UserRegisterDto registerDto) {

        // 检查用户名是否已存在
        if (userRepository.findByUsername(registerDto.getUsername()).isPresent()) {
            return ApiResponse.fail(400, "用户名已存在");
        }

        // 检查邮箱是否已存在
        User user = userRepository.findByEmail(registerDto.getEmail());
        if (user != null) {
            return ApiResponse.fail(400, "邮箱已存在");
        }

        // 创建新用户
        user = new User();
        user.setUsername(registerDto.getUsername());
        user.setPassword(passwordEncoder.encode(registerDto.getPassword())); // 加密存储密码
        user.setEmail(registerDto.getEmail());
        user.setFirstName(registerDto.getFirstName());
        user.setLastName(registerDto.getLastName());
        userRepository.save(user);

        // 返回成功响应
        return ApiResponse.success("注册成功", convertToResponseDto(user));
    }

    @Override
    public ApiResponse<UserResponseDto> login(UserLoginDto loginDto) {
        // 检查用户是否存在
        Optional<User> userOptional = userRepository.findByUsername(loginDto.getUsername());
        if (userOptional.isEmpty()) {
            return ApiResponse.fail(401, "用户名或密码错误");
        }

        // 检查密码是否正确
        User user = userOptional.get();
        if (!passwordEncoder.matches(loginDto.getPassword(), user.getPassword())) {
            return ApiResponse.fail(401, "用户名或密码错误");
        }

        // 返回成功响应
        return ApiResponse.success("登录成功", convertToResponseDto(user)); // 返回用户信息
    }


    private UserResponseDto convertToResponseDto(User user) {
        UserResponseDto dto = new UserResponseDto();
        dto.setId(user.getId());
        dto.setUsername(user.getUsername());
        dto.setEmail(user.getEmail());
        dto.setFirstName(user.getFirstName());
        dto.setLastName(user.getLastName());
        return dto;
    }
}
